package com.ruoyi.edit.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.edit.domain.VendorCourse;
import com.ruoyi.edit.service.IVendorCourseService;
import com.ruoyi.edit.service.OssService;
import com.ruoyi.edit.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.edit.domain.CourseContent;
import com.ruoyi.edit.service.ICourseContentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 活动课内容Controller
 * 
 * @author qiufeng
 * @date 2022-03-19
 */
@Controller
@RequestMapping("/system/courseContent")
public class CourseContentController extends BaseController
{
    private String prefix = "system/courseContent";

    @Autowired
    private ICourseContentService courseContentService;

    @Autowired
    private IVendorCourseService vendorCourseService;

    @Autowired
    private OssService ossService;

    @Value("${oss.path}")
    public  String path;

    @Value("${oss.url}")
    public  String ossUrl;

    @GetMapping()
    public String courseContent()
    {
        return prefix + "/courseContent";
    }

    /**
     * 查询活动课内容列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CourseContent courseContent)
    {
        startPage();
        List<CourseContent> list = courseContentService.selectCourseContentList(courseContent);
        return getDataTable(list);
    }

    /**
     * 导出活动课内容列表
     */
    @Log(title = "活动课内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CourseContent courseContent)
    {
        List<CourseContent> list = courseContentService.selectCourseContentList(courseContent);
        ExcelUtil<CourseContent> util = new ExcelUtil<CourseContent>(CourseContent.class);
        return util.exportExcel(list, "活动课内容数据");
    }

    /**
     * 新增活动课内容
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存活动课内容
     */
    @Log(title = "活动课内容", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CourseContent courseContent)
    {
        courseContentService.insertCourseContent(courseContent);
        return toAjax(1);
    }

    /**
     * 修改活动课内容
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CourseContent courseContent = courseContentService.selectCourseContentById(id);
        mmap.put("courseContent", courseContent);
        return prefix + "/edit";
    }

    /**
     * 修改保存活动课内容
     */
    @Log(title = "活动课内容", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CourseContent courseContent)
    {
        return toAjax(courseContentService.updateCourseContent(courseContent));
    }

    /**
     * 删除活动课内容
     */
    @Log(title = "活动课内容", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(courseContentService.deleteCourseContentByIds(ids));
    }

    /**
     * 删除活动课内容
     */
    @PostMapping( "/remove/{id}")
    @ResponseBody
    public AjaxResult removeId(@PathVariable("id") String id)
    {
        return toAjax(courseContentService.deleteCourseContentByIds(id));
    }



    /**
     * 新增修改保存活动课内容
     */
    @Log(title = "新增修改保存活动课内容", businessType = BusinessType.UPDATE)
    @PostMapping("/addAll")
    @ResponseBody
    public AjaxResult addAll(CourseContent courseContent)
    {
        return toAjax(courseContentService.addAll(courseContent));
    }

    /**
     * 预览
     */
    @GetMapping("/preview")
    public String preview( String id,String type ,ModelMap mmap)
    {
        CourseContent courseContent = courseContentService.getVendorPackageIdAndType(id,type);
        String content = courseContent.getContent();
        mmap.put("content", content);
        return prefix + "/preview";
    }

    @GetMapping("/courseList")
    public String courseList(String courseId,ModelMap mmap)
    {
        mmap.put("courseId", courseId);
        return prefix + "/courseList";
    }


    @GetMapping("/courseListV2")
    public String courseListV2(String courseId,ModelMap mmap)
    {
        mmap.put("courseId", courseId);
        return prefix + "/courseListV2";
    }

    @GetMapping("/addCourseContent")
    public String addCourseContent(String courseId,String detailsType,ModelMap mmap)
    {
        if (AssertUtil.isEmpty(detailsType)){
            detailsType= "0";
        }
        mmap.put("courseId", courseId);
        mmap.put("detailsType", detailsType);
        return prefix + "/addCourseContent";
    }

    @GetMapping("/courseParentList")
    public String courseParentList(String id,ModelMap mmap)
    {
        CourseContent courseContent = courseContentService.selectCourseContentById(id);
        mmap.put("id", id);
        mmap.put("courseId", courseContent.getCourseId());
        return prefix + "/courseParentList";
    }

    /**
     * 修改保存活动课内容
     */
    @PostMapping("/getTitle")
    @ResponseBody
    public AjaxResult getTitle(String id)
    {
        String name  = getName(id,"");
        return AjaxResult.success(name);
    }
    String getName(String id,String name){
        CourseContent courseContent = courseContentService.selectCourseContentById(id);
        if (!StringUtils.equals(courseContent.getParentId(),"0")){
            name = name + getName(courseContent.getParentId(),courseContent.getTitle()+"-");
        }else {
            name = name +courseContent.getTitle()+"-";
        }
        return name;
    }
    @GetMapping("/addParentCourseContent")
    public String addParentCourseContent(String id,ModelMap mmap)
    {
        CourseContent courseContent = courseContentService.selectCourseContentById(id);

        CourseContent courseContent1 = courseContentService.selectCourseContentById(courseContent.getParentId());
        if (Objects.nonNull(courseContent1)){
            mmap.put("menuStyle", courseContent1.getMenuStyle());
        }
        mmap.put("id", id);
        mmap.put("courseId", courseContent.getCourseId());

        return prefix + "/addParentCourseContent";
    }

    /**
     * 修改课程内容
     */
    @GetMapping("/editCourseContent/{id}")
    public String editCourseContent(@PathVariable("id") String id, ModelMap mmap)
    {
        CourseContent courseContent = courseContentService.selectCourseContentById(id);
        mmap.put("courseContent", courseContent);
        return prefix + "/editCourseContent";
    }


    @GetMapping("/coursePackageList")
    public String coursePackageList(String packageId,ModelMap mmap)
    {
        mmap.put("vendorPackageId", packageId);
        return prefix + "/coursePackageList";
    }


    @GetMapping("/addCoursePackageContent")
    public String addCoursePackageContent(String vendorPackageId,ModelMap mmap)
    {
        mmap.put("vendorPackageId", vendorPackageId);
        return prefix + "/addCoursePackageContent";
    }

    /**
     * 修改课程内容
     */
    @GetMapping("/editCoursePackageContent/{id}")
    public String editCoursePackageContent(@PathVariable("id") String id, ModelMap mmap)
    {
        CourseContent courseContent = courseContentService.selectCourseContentById(id);
        mmap.put("courseContent", courseContent);
        return prefix + "/editCoursePackageContent";
    }

    @GetMapping("/coursePackageParentList")
    public String coursePackageParentList(String id,ModelMap mmap)
    {
        CourseContent courseContent = courseContentService.selectCourseContentById(id);
        mmap.put("id", id);
        mmap.put("vendorPackageId", courseContent.getVendorPackageId());
        return prefix + "/coursePackageParentList";
    }

    @GetMapping("/addPackageParentCourseContent")
    public String addPackageParentCourseContent(String id,ModelMap mmap)
    {
        CourseContent courseContent = courseContentService.selectCourseContentById(id);
        mmap.put("id", id);
        mmap.put("vendorPackageId", courseContent.getVendorPackageId());
        return prefix + "/addPackageParentCourseContent";
    }


    @GetMapping("/coursePackageAppendixParentList")
    public String coursePackageAppendixParentList(String id,ModelMap mmap)
    {
        CourseContent courseContent = courseContentService.selectCourseContentById(id);
        mmap.put("id", id);
        mmap.put("vendorPackageId", courseContent.getVendorPackageId());
        return prefix + "/coursePackageAppendixParentList";
    }

    /**
     * 上传图片
     */
    @PostMapping("/importImg")
    @ResponseBody
    public String importImg(MultipartFile file)
    {
        String originalName = file.getOriginalFilename();
        String extension = originalName.substring(originalName.lastIndexOf("."));
        String fileName = "admin" + "_"+ System.currentTimeMillis() + extension;
        String url = ossService.aliyunOssUpload(path+ "common/"+ fileName,file);
        return ossUrl+url;
    }


    /**
     * 上传图片集合
     */
    @PostMapping("/importImgList")
    @ResponseBody
    public AjaxResult importImgList(List<MultipartFile> files)
    {
        List<String> urls = new ArrayList<String>();
        for (int i = 0; i < files.size(); i++) {
            MultipartFile file = files.get(i);
            String originalName = file.getOriginalFilename();
            String extension = originalName.substring(originalName.lastIndexOf("."));
            String fileName = "admin" + "_"+ System.currentTimeMillis() + extension;
            String url = ossService.aliyunOssUpload(path+ "common/"+ fileName,file);
            urls.add(ossUrl+url) ;
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("urls", StringUtils.join(urls, ","));
        return ajax;
    }

    @GetMapping("/addAppendixParentCourseContent")
    public String addAppendixParentCourseContent(String id,ModelMap mmap)
    {
        CourseContent courseContent = courseContentService.selectCourseContentById(id);
        mmap.put("id", id);
        mmap.put("vendorPackageId", courseContent.getVendorPackageId());
        return prefix + "/addAppendixParentCourseContent";
    }

    @GetMapping("/editAppendixParentContent/{id}")
    public String editAppendixParentContent(@PathVariable("id") String id, ModelMap mmap)
    {
        CourseContent courseContent = courseContentService.selectCourseContentById(id);
        mmap.put("courseContent", courseContent);
        return prefix + "/editAppendixParentContent";
    }

    @GetMapping("/imgDetails/{id}")
    public String imgDetails(@PathVariable("id") String id, ModelMap mmap)
    {
        CourseContent courseContent = courseContentService.selectCourseContentById(id);

        String[] list  = Convert.toStrArray(courseContent.getContent());
        List<String> lists = new ArrayList<>();
        for (int i = 0; i < list.length; i++) {
            lists.add(list[i]);
        }
        mmap.put("lists", lists);
        return prefix + "/imgDetails";
    }


    /**
     * 修改课程内容
     */
    @GetMapping("/editPackageParentContent/{id}")
    public String editPackageParentContent(@PathVariable("id") String id, ModelMap mmap)
    {
        CourseContent courseContent = courseContentService.selectCourseContentById(id);
        mmap.put("courseContent", courseContent);
        return prefix + "/editPackageParentContent";
    }


    /**
     * 删除关联的指定ID
     */
    @PostMapping("/del")
    @ResponseBody
    public String del(String contentId,String toolId)
    {
        CourseContent courseContent = courseContentService.selectCourseContentById(contentId);
        if (Objects.nonNull(courseContent.getTools())){
            String[] ids =  Convert.toStrArray(courseContent.getTools());
            for (int i = 0; i < ids.length; i++) {
                if (StringUtils.equals(ids[i],toolId)){
                    if (i == ids.length){
                        //数组缩容
                        ids = Arrays.copyOf(ids, ids.length-1);
                    }else {
                        ids[i] = ids[ids.length-1];
                        //数组缩容
                        ids = Arrays.copyOf(ids, ids.length-1);
                    }
                    break;
                }
            }
            String tools = "";
            for (int i = 0; i < ids.length; i++) {
                if (StringUtils.equals(tools,"")){
                    tools = tools + ids[i];
                }else {
                    tools = tools +","+  ids[i];
                }
            }
            courseContent.setTools(tools);
            courseContentService.updateCourseContent(courseContent);
            return tools;
        }else {
            return "";
        }
    }

//    /**
//     * 拷贝教保指引
//     */
//    @PostMapping("/copyCourseContentV2")
//    @ResponseBody
//    @ApiOperation("拷贝教保指引")
//    public void  list()
//    {
//
//        VendorCourse course = new VendorCourse();
//        course.setDeleted(0);
//        List<VendorCourse> vendorCourseList = vendorCourseService.selectVendorCourseList(course);
//        for (int i = 0; i < vendorCourseList.size(); i++) {
//            VendorCourse  course1 = vendorCourseList.get(i);
//            CourseContent courseContent  = new CourseContent();
//            courseContent.setDetailsType(1);
//            courseContent.setDeleted(0);
//            courseContent.setCourseId(course1.getId());
//            List<CourseContent> list = courseContentService.selectCourseContentList(courseContent);
//            if (list.size()==0){
//                courseContentService.copyWeekPackagesV2(courseContent.getCourseId(),course1.getCoursePackageId());
//            }
//        }
//    }





}
